package com.kczyjg.hybird

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.BarUtils
import java.io.File

class H5Activity : AppCompatActivity() {
    private lateinit var webView: H5WebView
    private lateinit var h5Url: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_h5)
        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, false)
        requestPermission()
        initView()
    }

    fun requestPermission() {
        val launcher =
            registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions(), { it ->
                val result = it.map { permission -> permission.value }.all { bool -> bool }
                if (result) {
                    GaoDeUtils.init(this@H5Activity)
                }
            })
        launcher.launch(
            arrayOf<String?>(
                Manifest.permission.CAMERA,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
            )
        )
    }

    fun initView() {
        RotationManagerUtil.start(this)
        webView = findViewById(R.id.webview)
        h5Url = "file:///" + cacheDir.absolutePath + File.separator + "h5/index.html"
        webView.loadUrl(h5Url)
    }

    override fun onBackPressed() {
        webView.evaluateJavascript("javascript:nativetojs_goBack()", null)
    }

    override fun onResume() {
        super.onResume()
        RotationManagerUtil.resume()
    }

    override fun onPause() {
        super.onPause()
        RotationManagerUtil.pause()
    }

    override fun onStop() {
        super.onStop()
        RotationManagerUtil.stop()
    }

    override fun onDestroy() {
        super.onDestroy()
        RotationManagerUtil.destroy()
    }

}